package com.castle.fortress.admin.exchange.rest;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.castle.fortress.admin.core.constants.GlobalConstants;
import com.castle.fortress.admin.exchange.entity.ExchangeVoucherEntity;
import com.castle.fortress.admin.exchange.dto.ExchangeVoucherDto;
import com.castle.fortress.admin.system.entity.SysUser;
import com.castle.fortress.admin.exchange.service.ExchangeVoucherService;
import com.castle.fortress.common.utils.ConvertUtil;
import com.castle.fortress.admin.utils.WebUtil;
import com.castle.fortress.common.entity.RespBody;
import com.castle.fortress.common.exception.BizException;
import com.castle.fortress.common.respcode.GlobalRespCode;
import com.castle.fortress.common.enums.OperationTypeEnum;
import com.castle.fortress.admin.core.annotation.CastleLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.logging.log4j.util.Strings;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 兑换券 api 控制器
 *
 * @author whc
 * @since 2022-12-06
 */
@Api(tags="兑换券api管理控制器")
@RestController
@RequestMapping("/api/exchange/exchangeVoucher")
public class ApiExchangeVoucherController {
    @Autowired
    private ExchangeVoucherService exchangeVoucherService;


    /**
     * 兑换券的分页展示
     * @param exchangeVoucherDto 兑换券实体类
     * @param currentPage 当前页
     * @param size  每页记录数
     * @return
     */
    @CastleLog(operLocation="兑换券-api",operType = OperationTypeEnum.QUERY)
    @ApiOperation("兑换券分页展示")
    @GetMapping("/page")
    public RespBody<IPage<ExchangeVoucherDto>> pageExchangeVoucher(ExchangeVoucherDto exchangeVoucherDto, @RequestParam(required = false) Integer currentPage, @RequestParam(required = false)Integer size){
        Integer pageIndex= currentPage==null? GlobalConstants.DEFAULT_PAGE_INDEX:currentPage;
        Integer pageSize= size==null? GlobalConstants.DEFAULT_PAGE_SIZE:size;
        Page<ExchangeVoucherDto> page = new Page(pageIndex, pageSize);

        IPage<ExchangeVoucherDto> pages = exchangeVoucherService.pageExchangeVoucher(page, exchangeVoucherDto);
        return RespBody.data(pages);
    }

    /**
     * 兑换券保存
     * @param exchangeVoucherDto 兑换券实体类
     * @return
     */
    @CastleLog(operLocation="兑换券-api",operType = OperationTypeEnum.INSERT)
    @ApiOperation("兑换券保存")
    @PostMapping("/save")
    public RespBody<String> saveExchangeVoucher(@RequestBody ExchangeVoucherDto exchangeVoucherDto){
        if(exchangeVoucherDto == null ){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
            ExchangeVoucherEntity exchangeVoucherEntity = ConvertUtil.transformObj(exchangeVoucherDto,ExchangeVoucherEntity.class);
        if(exchangeVoucherService.save(exchangeVoucherEntity)){
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 兑换券编辑
     * @param exchangeVoucherDto 兑换券实体类
     * @return
     */
    @CastleLog(operLocation="兑换券-api",operType = OperationTypeEnum.UPDATE)
    @ApiOperation("兑换券编辑")
    @PostMapping("/edit")
    public RespBody<String> updateExchangeVoucher(@RequestBody ExchangeVoucherDto exchangeVoucherDto){
        if(exchangeVoucherDto == null || exchangeVoucherDto.getId() == null || exchangeVoucherDto.getId().equals(0L)){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
            ExchangeVoucherEntity exchangeVoucherEntity = ConvertUtil.transformObj(exchangeVoucherDto,ExchangeVoucherEntity.class);
        if(exchangeVoucherService.updateById(exchangeVoucherEntity)){
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 兑换券删除
     * @param ids 兑换券id集合
     * @return
     */
    @CastleLog(operLocation="兑换券-api",operType = OperationTypeEnum.DELETE)
    @ApiOperation("兑换券删除")
    @PostMapping("/delete")
    public RespBody<String> deleteExchangeVoucher(@RequestBody List<Long> ids){
        if(ids == null || ids.isEmpty()){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        if(exchangeVoucherService.removeByIds(ids)) {
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 兑换券详情
     * @param id 兑换券id
     * @return
     */
    @CastleLog(operLocation="兑换券-api",operType = OperationTypeEnum.QUERY)
    @ApiOperation("兑换券详情")
    @GetMapping("/info")
    public RespBody<ExchangeVoucherDto> infoExchangeVoucher(@RequestParam Long id){
        if(id == null){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
            ExchangeVoucherEntity exchangeVoucherEntity = exchangeVoucherService.getById(id);
            ExchangeVoucherDto exchangeVoucherDto = ConvertUtil.transformObj(exchangeVoucherEntity,ExchangeVoucherDto.class);
        return RespBody.data(exchangeVoucherDto);
    }


}
